package com.linkstec.mot.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期帮助类
 * 
 * @author PENGYAO
 * 
 */
public class DateUtil {
	/**
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(long date, String pattern) {
		return format(new Date(date), pattern);
	}

	/**
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(Date date, String pattern) {
		DateFormat df = createDateFormat(pattern);
		return df.format(date);
	}

	/**
	 * 
	 * @param pattern
	 * @return
	 */
	private static DateFormat createDateFormat(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		// TimeZone gmt = TimeZone.getTimeZone("GMT");
		// sdf.setTimeZone(gmt);
		sdf.setLenient(true);
		return sdf;
	}

	/**
	 * 
	 * @param s
	 * @return
	 */
	public static Date parseString(String s) {
		if (null == s) {
			return null;
		}
		return parseString(s, null);
	}

	/**
	 * 转换日期格式
	 * 
	 * @param s
	 * @param format
	 * @return
	 */
	public static Date parseString(String s, String format) {
		if (s == null || s.length() <= 0) {
			return null;
		}
		SimpleDateFormat sdf;
		try {
			if (format == null) {
				format = "yyyy-MM-dd HH:mm:ss";
			}
			sdf = new SimpleDateFormat(format);
			return sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
}
